# 小红书内容生成应用启动脚本 (PowerShell版本)

Write-Host "正在启动小红书内容生成应用..." -ForegroundColor Green
Write-Host ""

# 获取脚本所在目录
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ScriptDir

# 激活虚拟环境
Write-Host "激活虚拟环境..." -ForegroundColor Yellow
$VenvActivate = Join-Path $ScriptDir "venv\Scripts\Activate.ps1"

if (Test-Path $VenvActivate) {
    try {
        & $VenvActivate
        Write-Host "虚拟环境已激活" -ForegroundColor Green
        Write-Host ""
    }
    catch {
        Write-Host "错误：无法激活虚拟环境" -ForegroundColor Red
        Write-Host "错误详情：$($_.Exception.Message)" -ForegroundColor Red
        Write-Host "请确保虚拟环境已正确安装在 venv 目录中" -ForegroundColor Yellow
        Read-Host "按任意键退出"
        exit 1
    }
}
else {
    Write-Host "错误：找不到虚拟环境激活脚本" -ForegroundColor Red
    Write-Host "路径：$VenvActivate" -ForegroundColor Yellow
    Write-Host "请确保虚拟环境已正确安装在 venv 目录中" -ForegroundColor Yellow
    Read-Host "按任意键退出"
    exit 1
}

# 检查app.py是否存在
$AppFile = Join-Path $ScriptDir "app.py"
if (-not (Test-Path $AppFile)) {
    Write-Host "错误：找不到 app.py 文件" -ForegroundColor Red
    Write-Host "路径：$AppFile" -ForegroundColor Yellow
    Read-Host "按任意键退出"
    exit 1
}

# 运行Python应用
Write-Host "启动应用程序..." -ForegroundColor Yellow
try {
    python app.py
    $ExitCode = $LASTEXITCODE
    
    if ($ExitCode -eq 0) {
        Write-Host ""
        Write-Host "应用程序正常退出" -ForegroundColor Green
    }
    else {
        Write-Host ""
        Write-Host "应用程序退出时出现错误 (退出代码: $ExitCode)" -ForegroundColor Red
    }
}
catch {
    Write-Host ""
    Write-Host "运行应用程序时发生异常：$($_.Exception.Message)" -ForegroundColor Red
}

Read-Host "按任意键退出"